var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/functionalComponent/antd/Button.tsx
var Button_exports = {};
__export(Button_exports, {
  Button: () => Button,
  ConfirmButton: () => ConfirmButton
});
module.exports = __toCommonJS(Button_exports);
var import_antd = require("antd");
var import_react = __toESM(require("react"));
var import_util = require("../../util");
function useButtonClick({
  loading = false,
  onClick,
  containerId,
  delay = 120,
  key,
  getClickParams
}) {
  const [btnLoading, setBtnLoading] = (0, import_util.useRefState)(loading);
  const isClicking = (0, import_react.useRef)(0);
  const refreshClicking = () => {
    isClicking.current = 2;
    setTimeout(() => {
      isClicking.current = 0;
    }, delay);
  };
  const btnClick = (0, import_util.useRefCallback)(async (e) => {
    if (isClicking.current) {
      return;
    }
    isClicking.current = 1;
    const params = await (getClickParams == null ? void 0 : getClickParams());
    const p = onClick == null ? void 0 : onClick({ ...e, containerId, key, ...params });
    if (import_util.iup.isPromise(p)) {
      setTimeout(() => {
        if (isClicking.current === 1) {
          setBtnLoading(true);
        }
      });
      try {
        return await p;
      } catch (e2) {
        console.log(e2);
      } finally {
        setBtnLoading(false);
        refreshClicking();
      }
    } else {
      refreshClicking();
      return p;
    }
  });
  (0, import_react.useEffect)(() => {
    setBtnLoading(loading);
  }, [loading]);
  return { loading: btnLoading, onClick: btnClick };
}
var Button = (0, import_util.compHoc)((props) => {
  const { outRef, observer, containerId, getClickParams, ...others } = props;
  const key = others.originid || others["data-cid"] || others.id;
  const { loading, onClick } = useButtonClick({
    loading: others.loading,
    onClick: others.onClick,
    containerId: containerId ?? key,
    key,
    getClickParams
  });
  return /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { ref: outRef, ...others, onClick, loading });
}, "Button");
var ConfirmButton = (0, import_util.compHoc)(
  ({
    children,
    size = "middle",
    type = "primary",
    buttonStyle,
    onBeforeOpen,
    onConfirm,
    onCancel,
    danger = false,
    observer,
    buttonProps,
    ...props
  }) => {
    const [visible, setVisible] = (0, import_react.useState)(false);
    const [confirmLoading, setConfirmLoading] = (0, import_react.useState)(false);
    const [btnLoading, setBtnLoading] = (0, import_react.useState)(false);
    const handleOk = async () => {
      setConfirmLoading(true);
      await (onConfirm == null ? void 0 : onConfirm());
      setVisible(false);
      setConfirmLoading(false);
    };
    const handleCancel = () => {
      setVisible(false);
      setConfirmLoading(false);
      onCancel == null ? void 0 : onCancel();
    };
    const onVisibleChange = async (value, e) => {
      if (!e && !confirmLoading) {
        if (value && onBeforeOpen) {
          let condition = onBeforeOpen(e);
          if (import_util.iup.isPromise(condition)) {
            setBtnLoading(true);
            condition = await condition;
            setBtnLoading(false);
          }
          if (condition === false) {
            return;
          }
        }
        setVisible(value);
      }
    };
    return /* @__PURE__ */ import_react.default.createElement(
      import_antd.Popconfirm,
      {
        ...props,
        onOpenChange: onVisibleChange,
        open: visible,
        style: { minWidth: 300 },
        onConfirm: handleOk,
        cancelButtonProps: { size: "middle", ...props.cancelButtonProps },
        okButtonProps: { loading: confirmLoading, size: "middle", ...props.okButtonProps },
        onCancel: handleCancel
      },
      /* @__PURE__ */ import_react.default.createElement(
        import_antd.Button,
        {
          style: buttonStyle,
          type,
          disabled: props.disabled,
          ...buttonProps,
          loading: btnLoading,
          danger,
          size,
          children
        }
      )
    );
  }
);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  Button,
  ConfirmButton
});
